define([ 'util/Knockout' ], function(ko) {
	"use strict";
	
	return new function() {
		var self = this;

		self.contains = function(array, item, equals) {
			var contains = false;
			
			if(equals == undefined) {
				equals = self.equals;
			}
			
			ko.utils.arrayForEach(array, function(arrayItem) {
				if(equals(arrayItem, item)) {
					contains = true;
					return;
				}
			});
			
			return contains;
		}; 
		
		self.arrayExecuteChanges = function(originalArray, newElements, equals) {
						
			var hasChanged = false;
			var elements = self.minus(newElements, originalArray(), equals);
			
			//Add new elements
			if(elements.length != 0) {
				hasChanged = true;
				ko.utils.arrayPushAll(originalArray(), elements);
			}
			
			//Remove old elements
			elements = self.minus(originalArray(), newElements, equals);
			if(elements.length != 0) {
				hasChanged = true;
				originalArray.removeAll(elements);
			}
			
			if(hasChanged) {
				originalArray.valueHasMutated();
			}
	
		};

		self.minus = function(source, array, equals) {
			
			if (array.length == 0) {
				return source;
			}

			if(equals == undefined) {
				equals = self.equals;
			}
			
			return ko.utils.arrayFilter(source, function(item) {
				return null == ko.utils.arrayFirst(array, function(arrayItem) {
					return equals(item, arrayItem);
				});
			});

		};
		
		self.equals = function(item1, item2) {
			if (item1['equals'] == undefined && item2['equals'] == undefined) {return false;}
			return item1['equals'].call(item1, item2);
		};
				
	};
});